# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>
# Contributor: Raed Rizqie <raed.rizqie@gmail.com>

_realname=angleproject
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.1.r21358.2e285bb5
pkgrel=5
pkgdesc='A conformant OpenGL ES implementation for Windows, Mac, Linux, iOS and Android (mingw-w64)'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url='https://chromium.googlesource.com/angle/angle'
license=('spdx:BSD-3-Clause')
depends=("${MINGW_PACKAGE_PREFIX}-jsoncpp"
         "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-rapidjson"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-egl-headers"
         "${MINGW_PACKAGE_PREFIX}-gles-headers")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-gn"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-python"
             "git")
_commit=("2e285bb591f78af3b5b1f83617e06f9ef9067924")
options=('!buildflags')
source=("${_realname}::git+https://chromium.googlesource.com/angle/angle.git#commit=${_commit}"
        "bare-clones/build::git+https://chromium.googlesource.com/chromium/src/build.git#commit=ccb49e801879c107ed6e96a84eb227f65ce4823b"
        "bare-clones/zlib::git+https://chromium.googlesource.com/chromium/src/third_party/zlib.git#commit=90e67ba3f8998a3532fc8e3db9539aada8060d43"
        "bare-clones/clang::git+https://chromium.googlesource.com/chromium/src/tools/clang.git#commit=64e9f9321c450d938fec79ef8e4431fd1a08f5ce"
        "swiftshader.zip::https://github.com/google/swiftshader/archive/0ba0b45490cd209448c5f976f41b34a746c9de5d.zip"
        "bare-clones/vulkan_memory_allocator::git+https://chromium.googlesource.com/external/github.com/GPUOpen-LibrariesAndSDKs/VulkanMemoryAllocator.git#commit=ebe84bec02c041d28f902da0214bf442743fc907"
        "bare-clones/vulkan-deps::git+https://chromium.googlesource.com/vulkan-deps.git#commit=23a32754e71562453af68898e6918e06172d4c46"
        "bare-clones/spirv-headers::git+https://chromium.googlesource.com/external/github.com/KhronosGroup/SPIRV-Headers.git#commit=3469b164e25cee24435029a569933cb42578db5d"
        "bare-clones/spirv-tools::git+https://chromium.googlesource.com/external/github.com/KhronosGroup/SPIRV-Tools.git#commit=310a67020a7d67be4fdf1b4bfa9bb85f985c6fd7"
        "bare-clones/vulkan-headers::git+https://chromium.googlesource.com/external/github.com/KhronosGroup/Vulkan-Headers.git#commit=b6a29e5ca865f48368f6b2f170adb89975bb0be1"
        "bare-clones/vulkan-loader::git+https://chromium.googlesource.com/external/github.com/KhronosGroup/Vulkan-Loader.git#commit=a1d9d485ce1a66adc9a584ea79bc3829203863b6"
        "bare-clones/vulkan-tools::git+https://chromium.googlesource.com/external/github.com/KhronosGroup/Vulkan-Tools.git#commit=247c806c93c720488daa0bc86acd5b6f3a0e14f9"
        "bare-clones/vulkan-validation-layers::git+https://chromium.googlesource.com/external/github.com/KhronosGroup/Vulkan-ValidationLayers.git#commit=9086fcc46a679c334f9ede021a65215981df776a"
        001-add-mingw-toolchain.patch
        002-buildflags-fixes.patch
        003-angle-src-fixes.patch
        004-swiftshader-updates.patch
        005-vulkan-memory-allocator-updates.patch
        006-spirv-updates.patch
        007-vulkan-loader-updates.patch
        008-arm64-mingw-intrinsic.patch
        jpeg.gn
        jsoncpp.gn
        png.gn
        rjson.gn
        zlib.gn)
sha256sums=('SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'ea1e3f0cc030911596b3fc4753a9228ea3a5a4eba6e21bb34b7a9867a3702738'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            '16db1feedcb5d2e8e1a9b20ac96f0be2f56d2419001de99cfd3d4508a2ec4242'
            '1ebc64c413b774c7afca13cbff255dca2d545b3817d08af0cfe7fd3668dd83a3'
            '04bcb05dc2f349639bf4e62ee92d758b756e56532ceb9184c2d7dca01e67a38e'
            'c3ee4b4194c041db225bcf3f699bf1042ddc0c845b1da142ca07a8bf1f7f80f8'
            'ced98a61919f7fdb10dbff26dfbd4ea11b034eaaeb3433f0fa5c4c998519e171'
            '276799dbae43ab2577547dcc7912353619ade2ba878b28947ddef545f48afffa'
            '7fc7f35aedc101fe325c161c42d2966f974ec789025750705ceb0caf259fc1f6'
            '6d5c3d7888a671059d378e955c86d297dede5e6eee0d6aa0aeb12eb84c9e8ca1'
            'bf878de3203a62e2a7f81bd4cd79237adc74804b4b692d9277da005ed56d4f2a'
            '86013781c2700219d4f64d7ac34ad16c40fcca9a641371385f67f642e23c643b'
            'f0fb05348bf2de599eff35e2d35e3336b9720b6bd2799af9d186c05ff45d34f0'
            '044afff420a1c8ad896f79fb8ae03878f6063bdb0a25dab0b8e4bff092e3d3ac'
            '4dc3baf79ba7c95b52cbbafce81b26e6d165d4ae7e0dcc2eabe0250466edf178')
noextract=("swiftshader.zip")

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -p1 -i "${srcdir}/${_patch}"
  done
}

pkgver() {
  cd ${_realname}
  local _major=$(head -n 14 src/common/angle_version.h | grep 'ANGLE_MAJOR_VERSION' | sed -e 's/.* //' | tr '\n' '.' | sed 's/.$/\n/')
  local _minor=$(head -n 15 src/common/angle_version.h | grep 'ANGLE_MINOR_VERSION' | sed -e 's/.* //' | tr '\n' '.' | sed 's/.$/\n/')
  printf "%s.%s.r%s.%s" "$_major" "$_minor" "$(git rev-list --count HEAD)" "$(git rev-parse --short=8 "${_commit}")"
}

prepare() {
  echo ":: Patching build directory"
  cd ${srcdir}/build
  patch -p1 -i ${srcdir}/001-add-mingw-toolchain.patch
  python ${srcdir}/build/util/lastchange.py -o ${srcdir}/build/util/LASTCHANGE
  rm -rf ${srcdir}/${_realname}/build
  ln -sf ${srcdir}/build ${srcdir}/${_realname}
  echo "checkout_google_benchmark = false" > ${srcdir}/${_realname}/build/config/gclient_args.gni

  echo ":: Patching zlib directory"
  mkdir -p ${srcdir}/${_realname}/third_party/zlib
  ln -sf ${srcdir}/zlib.gn ${srcdir}/${_realname}/third_party/zlib/BUILD.gn
  ln -sf ${srcdir}/zlib/google ${srcdir}/${_realname}/third_party/zlib

  echo ":: Extracting SwiftShader"
  cd ${srcdir}
  bsdtar -xf swiftshader.zip || bsdtar -xf swiftshader.zip
  mv swiftshader-* SwiftShader

  echo ":: Patching SwiftShader directory"
  cd ${srcdir}/SwiftShader
  patch -p1 -i ${srcdir}/004-swiftshader-updates.patch
  patch -p1 -i ${srcdir}/008-arm64-mingw-intrinsic.patch
  ln -sf ${srcdir}/SwiftShader ${srcdir}/${_realname}/third_party

  echo ":: Patching vulkan_memory_allocator directory"
  cd ${srcdir}/vulkan_memory_allocator
  patch -p1 -i ${srcdir}/005-vulkan-memory-allocator-updates.patch
  ln -sf ${srcdir}/vulkan_memory_allocator ${srcdir}/${_realname}/third_party

  echo ":: Patching spirv-tools directory"
  cd ${srcdir}/spirv-tools
  patch -p1 -i ${srcdir}/006-spirv-updates.patch

  echo ":: Patching vulkan-loader directory"
  cd ${srcdir}/vulkan-loader
  patch -p1 -i ${srcdir}/007-vulkan-loader-updates.patch

  echo ":: Copying vulkan-deps files"
  ln -sf ${srcdir}/vulkan-deps ${srcdir}/${_realname}/third_party
  cp -rf ${srcdir}/spirv-headers ${srcdir}/${_realname}/third_party/vulkan-deps/spirv-headers/src
  cp -rf ${srcdir}/spirv-tools ${srcdir}/${_realname}/third_party/vulkan-deps/spirv-tools/src
  cp -rf ${srcdir}/vulkan-headers ${srcdir}/${_realname}/third_party/vulkan-deps/vulkan-headers/src
  cp -rf ${srcdir}/vulkan-loader ${srcdir}/${_realname}/third_party/vulkan-deps/vulkan-loader/src
  cp -rf ${srcdir}/vulkan-tools ${srcdir}/${_realname}/third_party/vulkan-deps/vulkan-tools/src
  cp -rf ${srcdir}/vulkan-validation-layers ${srcdir}/${_realname}/third_party/vulkan-deps/vulkan-validation-layers/src

  echo ":: Soft linking extra repos"
  mkdir -p ${srcdir}/${_realname}/third_party/libjpeg
  ln -sf ${srcdir}/jpeg.gn ${srcdir}/${_realname}/third_party/libjpeg/BUILD.gn
  mkdir -p ${srcdir}/${_realname}/third_party/jsoncpp
  ln -sf ${srcdir}/jsoncpp.gn ${srcdir}/${_realname}/third_party/jsoncpp/BUILD.gn
  rm -f ${srcdir}/${_realname}/third_party/libpng/BUILD.gn
  ln -sf ${srcdir}/png.gn ${srcdir}/${_realname}/third_party/libpng/BUILD.gn
  rm -f ${srcdir}/${_realname}/third_party/rapidjson/BUILD.gn
  ln -sf ${srcdir}/rjson.gn ${srcdir}/${_realname}/third_party/rapidjson/BUILD.gn
  ln -sf ${srcdir}/clang ${srcdir}/${_realname}/tools

  echo ":: Patching angle source"
  cd ${srcdir}/${_realname}
  apply_patch_with_msg \
    002-buildflags-fixes.patch \
    003-angle-src-fixes.patch

  echo ":: Generating angle_commit.h"
  python src/commit_id.py gen src/common/angle_commit.h
}

build() {
  cd ${_realname}

  local _target=Release
  local _debug=false
  if check_option "debug" "y"; then
    _target=Debug
    _debug=true
  fi

  local _arch=x64
  if [[ ${CARCH} == aarch64 ]]; then
    _arch=arm64
  elif [[ ${CARCH} == i686 ]]; then
    _arch=x86
  fi

  local _clang=false
  if [[ ${MSYSTEM} == CLANG* ]]; then
    _clang=true
  fi

  local _swiftshader=false
  if [[ ${MSYSTEM} == CLANG* ]] && [[ ${CARCH} != aarch64 ]]; then
    _swiftshader=true
  fi

  if [[ ${MSYSTEM} == CLANG* ]]; then
    export CXX=clang++
    export CC=clang
    export AR=llvm-ar
  else
    export CXX=g++
    export CC=gcc
    export AR=ar
  fi

  ${MINGW_PREFIX}/bin/gn gen out/${_target}-${MSYSTEM} --args="
    target_cpu=\"${_arch}\"
    is_debug=${_debug}
    is_clang=${_clang}
    is_component_build=true
    angle_enable_abseil=false
    angle_enable_metal=false
    angle_enable_d3d11_compositor_native_window=false
    angle_enable_cl=false
    angle_shared_libvulkan=false
    angle_enable_swiftshader=${_swiftshader}
    chrome_pgo_phase=0
    use_custom_libcxx=false
    use_sysroot=false
    treat_warnings_as_errors=false"

  ${MINGW_PREFIX}/bin/ninja -C out/${_target}-${MSYSTEM}
}

package() {
  cd ${_realname}

  local _target=Release
  if check_option "debug" "y"; then
    _target=Debug
  fi

  mkdir -p ${pkgdir}${MINGW_PREFIX}/{bin,lib,include/{EGL,GLES2}}

  install -Dm755 out/${_target}-${MSYSTEM}/lib*.dll ${pkgdir}${MINGW_PREFIX}/bin/
  install -Dm755 out/${_target}-${MSYSTEM}/lib*.dll.a ${pkgdir}${MINGW_PREFIX}/lib/
  install -Dm755 out/${_target}-${MSYSTEM}/obj/lib*.a ${pkgdir}${MINGW_PREFIX}/lib/

  # Copy public ANGLE headers
  install -Dm644 include/angle_gl.h ${pkgdir}${MINGW_PREFIX}/include
  install -Dm644 include/angle_windowsstore.h ${pkgdir}${MINGW_PREFIX}/include
  install -Dm644 include/EGL/eglext_angle.h ${pkgdir}${MINGW_PREFIX}/include/EGL
  install -Dm644 include/GLES2/gl2ext_angle.h ${pkgdir}${MINGW_PREFIX}/include/GLES2

  # Copy license
  install -Dm644 LICENSE ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
